package com.els.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.els.ElsConnection;
import com.els.ElsPool;
import com.google.gson.JsonObject;

public class ElsBaseDAO {
	protected Logger logger=Logger.getLogger(this.getClass());
	protected String indexName;
	protected String typeName;
	protected String id=null;
	protected ElsPool ePool;
	protected ElsBaseDAO(){
		setTypeName();
	}
	protected void setIndexName(String indexName) {
		this.indexName = indexName;
	}
	protected void setTypeName() {
		String typeName=this.getClass().getSimpleName();
		typeName=typeName.substring(0,typeName.lastIndexOf("DAO"));
		this.typeName = typeName.toLowerCase();
	}
	protected ElsConnection createConnection(){
		return ePool.getConnection();
	}
	protected void closeConnection(ElsConnection connection){
		ePool.backConnection(connection);
	}
	public String insertOne(JsonObject obj){
		ElsConnection ec = ePool.getConnection();
		String _id=ec.insertOne(indexName, typeName, id, obj);
		ePool.backConnection(ec);
		return _id;
	}
	public boolean deleteOne(String _id){
		ElsConnection ec = ePool.getConnection();
		boolean result=ec.deleteOne(indexName, typeName, _id);
		ePool.backConnection(ec);
		return result;
	}
	public boolean updateOne(String _id,JsonObject obj){
		ElsConnection ec = ePool.getConnection();
		boolean result=ec.updateOne(indexName, typeName, _id, obj);
		ePool.backConnection(ec);
		return result;
	}
	public JsonObject selectOne(String _id,JsonObject obj){
		ElsConnection ec = ePool.getConnection();
		JsonObject result=ec.selectOne(indexName, typeName, _id, obj);
		ePool.backConnection(ec);
		return result;
	}
	public List<JsonObject> queryAll(){
		ElsConnection ec = ePool.getConnection();
		List<JsonObject> results=ec.queryAll(indexName, typeName);
		ePool.backConnection(ec);
		return results;
	}
}
